package com.yztc.youbadbad.ui.homepage;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.google.gson.Gson;
import com.yztc.youbadbad.App;
import com.yztc.youbadbad.R;
import com.yztc.youbadbad.adapter.OtherHomepageRecyclerAdapter;
import com.yztc.youbadbad.bean.homepage.Other2Bean;
import com.yztc.youbadbad.bean.homepage.Other2GoodBean;
import com.yztc.youbadbad.bean.homepage.OtherAdBean;
import com.yztc.youbadbad.bean.homepage.OtherGoodBean;
import com.yztc.youbadbad.bean.homepage.OtherHotBean;
import com.yztc.youbadbad.bean.homepage.OtherImageBean;
import com.yztc.youbadbad.bean.homepage.OtherSecondaryBean;
import com.yztc.youbadbad.bean.homepage.ProductBean;
import com.yztc.youbadbad.helper.DividerItemDecoration;
import com.yztc.youbadbad.presenter.IOtherPresenter;
import com.yztc.youbadbad.presenter.OtherPresenterImpl;
import com.yztc.youbadbad.ui.homepage.interfaces.IOtherView;
import com.yztc.youbadbad.utils.ToastUtils;
import com.yztc.youbadbad.utils.UIManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页其他的tab
 */
public class OtherHomeFragment extends Fragment
        implements IOtherView {
    @BindView(R.id.other_rv)
    RecyclerView otherRv;
    @BindView(R.id.progressBar)
    ProgressBar progressBar;

    private Context mContext = App.app;
    private int mPostion = 26;
    private OtherHomepageRecyclerAdapter mAdapter;
    private List<Object> mOtherList = new ArrayList<>();
    private IOtherPresenter mIOtherPresenter;

    public static OtherHomeFragment newInstance(int position) {
        OtherHomeFragment fragment = new OtherHomeFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mPostion = getArguments().getInt("position");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_other_home, container, false);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mOtherList.clear();
        loadData();
        initView();
    }


    private void initView() {
        mAdapter = new OtherHomepageRecyclerAdapter(mContext, mOtherList, this);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        otherRv.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
        otherRv.setAdapter(mAdapter);
        otherRv.setLayoutManager(manager);
    }

    //加载网络资源
    private void loadData() {
        mIOtherPresenter = new OtherPresenterImpl(this);
        mIOtherPresenter.getOtherInternetData(mPostion);

    }

    /**
     * @param i    下标
     * @param data JsonArray
     * @throws JSONException
     */
    private void getBeans(int i, JSONArray data) throws JSONException {
        JSONObject bean = data.getJSONObject(i);
        String str = bean.toString();
        Gson gson = new Gson();
        switch (i) {
            case 0://轮播图要一张
                OtherImageBean otherImageBean = gson.fromJson(str, OtherImageBean.class);
                OtherAdBean otherAdBean = otherImageBean.getData().getList().get(0);
                mOtherList.add(otherAdBean);//一张图片
                break;
            case 1://二级分类 要
                OtherSecondaryBean otherSecondaryBean = gson.fromJson(str, OtherSecondaryBean.class);
                List<Other2Bean> list2 = otherSecondaryBean.getData().getList();
                mOtherList.add(list2);
                break;
            case 2://功能入口模块
                break;
            case 3://秒杀专场
                break;
            case 4://固定常规
                break;
        }
    }

    /**
     * @param i    下标
     * @param data JsonArray
     * @throws JSONException
     */
    private void getBeans2(int i, JSONArray data) throws JSONException {
        JSONObject bean = data.getJSONObject(i);
        String str = bean.toString();
        Gson gson = new Gson();


        switch (i) {
            case 0://好物晒图
                OtherGoodBean otherGoodBean = gson.fromJson(str, OtherGoodBean.class);
                Other2GoodBean other2GoodBean = otherGoodBean.getData();
                mOtherList.add(other2GoodBean);
                break;
            case 1:
                break;
            case 2://最热单品
                OtherHotBean hotBean = gson.fromJson(str, OtherHotBean.class);
                List<ProductBean> product_list = hotBean.getData().getList().getProduct_list();
                int size = product_list.size();
                mOtherList.addAll(product_list);
                break;
            case 3:
                break;
            case 4:
                break;
        }
    }

    @Override
    public void getOtherInternetData(String other) {
        mIOtherPresenter.getOther2InternetData(mPostion);
        try {
            JSONObject jsonObject = new JSONObject(other);
            JSONArray data = jsonObject.getJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                getBeans(i, data);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        mAdapter.notifyDataSetChanged();

    }

    @Override
    public void getOther2InternetData(String other) {

        try {
            JSONObject jsonObject = new JSONObject(other);
            JSONArray data = jsonObject.getJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                int j = i;
                getBeans2(i, data);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("tag", e.getMessage());
        }
    }

    @Override
    public void showProgressBar() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgressBar() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showErrorMessage() {
        ToastUtils.showEorrorToast(mContext);
    }


    /**
     * 商品点击事件监听 回调接口
     *
     * @param position
     */
    @Override
    public void otherProducetItemclicked(int position) {
        if (position >= 3) {//热门商品
            ProductBean productBean = (ProductBean) mOtherList.get(position);
            UIManager.startProduct_DetialInfo(getParentFragment().getActivity(), productBean);//跳转商品详情画面
        }
    }
}
